package com.Maykk.Demo10;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @ClassName: Timer
 * @Description: TODO Timer计时器 - Thread线程
 * @Author: MaykkStar@163.com
 */
public class TimerTest {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 20; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + " i= " + i);
                }
            }
        }, "多线程").start();

        /*
         * Timer 会自己干自己的活，不会抢占线程运行
         * */
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    System.out.println(Thread.currentThread().getName() + " i = " + i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.gc();
            }
        }, 2000);
    }
}
